package org.csu.bbs.controller;

import org.csu.bbs.entity.Admin;
import org.csu.bbs.entity.Signon;
import org.csu.bbs.entity.UserInfo;
import org.csu.bbs.result.User;
import org.csu.bbs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    UserService userServiceImpl;

    @CrossOrigin
    @DeleteMapping("api/alluser/{userid}")
    @ResponseBody
    public int delete(@PathVariable(value = "userid") String userid){
        return userServiceImpl.DeleteOne(userid);
    }
    @CrossOrigin
    @GetMapping("api/userr/search")
    @ResponseBody
    public List<UserInfo> search(@RequestParam("keywords") String keywords ){
        if ("".equals(keywords)) {
            return null;
        } else {
            return userServiceImpl.search(keywords);
        }
    }

    @CrossOrigin
    @GetMapping("api/userr/all")
    @ResponseBody
    public List<UserInfo> searchAll(){
        return userServiceImpl.searchAll();
    }

    @CrossOrigin
    @PutMapping("api/userr/change")
    @ResponseBody
    public List<UserInfo> changeBan(@RequestBody UserInfo userInfo){
        return  userServiceImpl.changeBan(userInfo);

    }
    @CrossOrigin
    @PutMapping("api/userr/Authority")
    @ResponseBody
    public List<UserInfo> changeAuthority(@RequestBody UserInfo userInfo){
        return  userServiceImpl.changeAuthority(userInfo);
    }

    @CrossOrigin
    @PostMapping("/api/user/Login")
    @ResponseBody
    public User Login(@RequestBody Signon signon){
        System.out.println(signon);
        User user = userServiceImpl.GetUserByUsernameAndPassword(signon.getUserid(),signon.getPassword());
        System.out.println(user);
        return user;
    }

    @CrossOrigin
    @PutMapping("/api/user")
    @ResponseBody
    public User EditUser(@RequestBody User user){
        return userServiceImpl.updateUser(user);
    }

    @CrossOrigin
    @PostMapping  ("api/user/login")
    @ResponseBody
    public boolean login1(@RequestBody Admin admin) {
        return userServiceImpl.GetByUsernameAndPassword(admin.getUsername(), admin.getPassword()) != null;
    }

    @CrossOrigin
    @GetMapping("/api/user/{id}")
    @ResponseBody
    public User GetUser(@PathVariable(value = "id") String id){
        User user = userServiceImpl.GetUserById(id);
        return user;
    }

    @CrossOrigin
    @PostMapping("/api/user/header")
    @ResponseBody
    public String ViewHeader(@RequestBody Object postsList){
//        return userServiceImpl.GetHeader(postsList);
        return null;
    }
}
